package cn.itsource.aigou.controller;

import cn.itsource.aigou.client.RedisClient;
import cn.itsource.aigou.util.AjaxResult;
import cn.itsource.aigou.util.RedisUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ReidsController implements RedisClient {

    @Override
    @PostMapping("/redis")
    public AjaxResult set(@RequestParam("key") String key,@RequestParam("value") String value) {

        try {
            RedisUtils.INSTANCE.set(key,value);
            return AjaxResult.me().setSuccess(true).setMessage("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            // getMessage(获取错误代码)
            return AjaxResult.me().setSuccess(false).setMessage("保存失败"+e.getMessage());
        }
    }

    @Override
    @GetMapping("/redis")
    public AjaxResult get(@RequestParam("key") String key) {

        try {
            String s = RedisUtils.INSTANCE.get(key);
            return AjaxResult.me().setSuccess(true).setMessage("获取成功").setRestObj(s);

        } catch (Exception e) {
            e.printStackTrace();
            // getMessage(获取错误代码)
            return AjaxResult.me().setSuccess(false).setMessage("网络异常,请联系管理员"+e.getMessage());
        }
    }

}
